#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.const import ReportDBLabel
from generaldb.gbase8a import LOGGER
from generaldb.gbase8a.common.const import RestoreSubJobName, AllStepEnum, EnvName, GbaseTaskLabel
from generaldb.gbase8a.job_manager import JobManager
from generaldb.gbase8a.param.restore_param_parser import RestoreParam
from generaldb.gbase8a.service.base_service import MetaService, AllowInterfaceMixin, PrerequisiteInterfaceMixin, \
    ExecuteInterfaceMixin, \
    PostInterfaceMixin
from generaldb.gbase8a.service.restore import prerequisite, restore, gen_sub
from generaldb.gbase8a.service.restore.restore_manager import RestoreManager


class RestoreService(MetaService):
    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.param = self.gen_param_obj()
        EnvName.DB_USER_NAME = "job_targetEnv_auth_authKey"
        EnvName.DB_PASSWORD = "job_targetEnv_auth_authPwd"
        EnvName.CUSTOM_SETTINGS = "job_targetEnv_auth_extendInfo_authCustomParams"
        self.restore_manager = RestoreManager(self.job_manager, self.param)

    def gen_param_obj(self):
        param = RestoreParam(self.param_dict)
        return param

    def get_steps(self):
        pass


@JobManager.register(AllStepEnum.ALLOW_RESTORE)
class AllowRestoreService(RestoreService, AllowInterfaceMixin):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)

    def get_steps(self):
        """
        """
        default_allow = [
            self.restore_manager.check_db_os_user,
            self.restore_manager.allow_execute_location
        ]
        allow_restore_map = {
            RestoreSubJobName.PREPARE_RESTORE: [],
        }
        sub_job = self.param.get_sub_job_name()
        steps = allow_restore_map.get(sub_job, default_allow)

        return steps


@JobManager.register(AllStepEnum.RESTORE_PREREQUISITE)
class PrerequisiteRestoreService(RestoreService, PrerequisiteInterfaceMixin):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.log_detail.update(prerequisite)

    def get_steps(self):
        """

        """
        steps = [
            # 通过执行show database 验证数据库户名密码
            self.restore_manager.show_databases,
            self.restore_manager.check_cluster_mode,
            self.restore_manager.check_gcrcman_process,
        ]
        return steps


@JobManager.register(AllStepEnum.RESTORE)
class RestoreExecuteService(RestoreService, ExecuteInterfaceMixin):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = True
        self.log_detail.update({
            "failed": ReportDBLabel.RESTORE_SUB_FAILED,
            "start": ReportDBLabel.RESTORE_SUB_START_COPY,
            "success": ReportDBLabel.SUB_JOB_SUCCESS
        })

    def get_steps(self):
        sub_job_steps_map = {
            RestoreSubJobName.PREPARE_RESTORE: [
                self.restore_manager.create_datapath_symlink,
            ],
            RestoreSubJobName.RESTORE: [
                self.restore_manager.can_restore,
                self.restore_manager.check_copies,
                self.restore_manager.drop_database,
                self.restore_manager.execute_restore,
                self.restore_manager.refresh_db_tables
            ]
        }
        sub_job = self.param.get_sub_job_name()
        steps = sub_job_steps_map.get(sub_job, [])
        return steps


@JobManager.register(AllStepEnum.RESTORE_POST)
class PostRestoreService(RestoreService, PostInterfaceMixin):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)

    def get_steps(self):
        steps = [
            self.restore_manager.delete_datapath_symlink
        ]
        return steps


@JobManager.register(AllStepEnum.RESTORE_GEN_SUB)
class GenSubRestoreService(RestoreService):

    def __init__(self, job_manager, param_dict):
        super().__init__(job_manager, param_dict)
        self.need_report = False
        self.log_detail.update({
            "failed": GbaseTaskLabel.GENERATE_SUBJOB_FAIL_LABEL,
            "success": GbaseTaskLabel.GENERATE_SUBJOB_SUCCESS_LABEL,
            "start": GbaseTaskLabel.EXECUTE_GENERATE_SUBJOB_LABEL
        })

    def get_steps(self):
        steps = [
            self.gen_sub_jobs
        ]
        return steps

    def gen_sub_jobs(self):
        self.restore_manager.gen_sub_jobs(self.job_manager.job_id)
